package example;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JApplet;

public class PigDemo extends JApplet implements MouseListener,
		MouseMotionListener {
	private static final long serialVersionUID = 1L;
	Pig pig[] = new Pig[9];
	int numberOfPig;
	Image bufferImage, cursorImage1, cursorImage2;
	Graphics adGraphics;
	MouseEvent aEvent;
	Cursor myCursor1 = null;
	Cursor myCursor2 = null;

	public void init() {
		setSize(400, 400);
		bufferImage = createImage(300, 300);
		adGraphics = bufferImage.getGraphics();
		cursorImage1 = Toolkit.getDefaultToolkit().createImage(
				"example/img/h1.GIF");
		myCursor1 = Toolkit.getDefaultToolkit().createCustomCursor(
				cursorImage1, new Point(0, 0), "Cursor1");
		cursorImage2 = Toolkit.getDefaultToolkit().createImage(
				"example/img/h2.GIF");
		myCursor2 = Toolkit.getDefaultToolkit().createCustomCursor(
				cursorImage2, new Point(0, 0), "Cursor1");
		this.addMouseListener(this);
		this.addMouseMotionListener(this);

		for (int i = 0; i < 9; i++) {
			pig[i] = new Pig(i % 3 * 100, i / 3 * 100, this);
			pig[i].setBackground(getImage(getCodeBase(),
					"example/img/FRAME.GIF"));
			pig[i].setPig(getImage(getCodeBase(), "example/img/PIG.GIF"));
		}
		RandomThread th1 = new RandomThread();
		th1.start();
	}

	public void paint(Graphics g) {
		for (int i = 0; i < 9; i++) {
			pig[i].paintBackground(adGraphics);
			pig[i].paintPig(adGraphics);
			pig[i].setVisible(false);
		}
		g.drawImage(bufferImage, 0, 0, this);

	}

	@Override
	public void update(Graphics g) {
		paint(g);
	}

	class RandomThread extends Thread {
		@Override
		public void run() {
			while (true) {
				try {
					sleep((int) (200 + Math.random() * 400));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				numberOfPig = (int) (Math.random() * 9);
				pig[numberOfPig].setVisible(true);
				repaint();
			}
		}
	}

	public void mouseClicked(MouseEvent arg0) {
		
	}

	public void mouseEntered(MouseEvent arg0) {
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent arg0) {
		this.setCursor(myCursor2);
	}

	public void mouseReleased(MouseEvent arg0) {
		this.setCursor(myCursor1);

	}

	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseMoved(MouseEvent arg0) {
		this.setCursor(myCursor1);
	}

}

class Pig {
	int xCoordinate;
	int yCoordinate;
	Image background;
	Image pig;
	PigDemo jApplet;
	boolean isVisible;

	public boolean isVisible() {
		return isVisible;
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public Pig(int coordinate, int coordinate2, PigDemo applet) {
		super();
		xCoordinate = coordinate;
		yCoordinate = coordinate2;
		jApplet = applet;
	}

	public int getXCoordinate() {
		return xCoordinate;
	}

	public void setXCoordinate(int coordinate) {
		xCoordinate = coordinate;
	}

	public int getYCoordinate() {
		return yCoordinate;
	}

	public void setYCoordinate(int coordinate) {
		yCoordinate = coordinate;
	}

	public Image getBackground() {
		return background;
	}

	public void setBackground(Image background) {
		this.background = background;
	}

	public Image getPig() {
		return pig;
	}

	public void setPig(Image pig) {
		this.pig = pig;
	}

	public PigDemo getJApplet() {
		return jApplet;
	}

	public void setJApplet(PigDemo applet) {
		jApplet = applet;
	}

	public void paintBackground(Graphics g) {
		g.drawImage(background, xCoordinate, yCoordinate, jApplet);

	}

	public void paintPig(Graphics g) {
		if (isVisible)
			g.drawImage(pig, xCoordinate + 12, yCoordinate + 17, jApplet);
	}
}